package com.sec.bnjey.database;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;

import com.sec.bnjey.utils.RbLog;

public class DbUtil {

	private static final String tag = "DbUtil";

	public static void copyDB(Context context, String dbName) {

		String DB_PATH = "/data/data/"
				+ context.getApplicationInfo().packageName + "/databases/";

		File file = new File(DB_PATH);

		if (!file.exists()) {
			boolean ret = file.mkdir();
			if (ret == false)
				return;
		}

		file = new File(DB_PATH + dbName);
		// copy only when db does not exist in local file system
		if (file.exists()) {
			RbLog.d(tag, "Already exist! " + DB_PATH + dbName);
			return;
		} else {
			RbLog.d(tag, "does not exist! " + DB_PATH + dbName);
		}

		// Open your local db as the input stream
		InputStream inputStream;
		try {
			inputStream = context.getAssets().open(dbName);

			// Open the empty db as the output stream
			OutputStream outputStream = new FileOutputStream(DB_PATH + dbName);

			// transfer bytes from the inputfile to the outputfile
			byte[] buffer = new byte[1024];
			int length;
			while ((length = inputStream.read(buffer)) > 0) {
				outputStream.write(buffer, 0, length);
			}

			// Close the streams
			outputStream.flush();
			outputStream.close();
			inputStream.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void testDB(Context context) {
		DtcDbAdapter mDBAdapter = null;
		mDBAdapter = DtcDbAdapter.getInstance(context.getApplicationContext());
		mDBAdapter.open();

		DtcInfo dtcInfo = mDBAdapter.get("P003A");
		if (dtcInfo != null)
			RbLog.d("XML", dtcInfo.getInfoEng());
		else
			RbLog.d("XML", "dtcInfo.getInfoEng() == null");

		mDBAdapter.close();

	}

	public static void testDB2(Context context) {
		DtcDbAdapter mDBAdapter = null;
		mDBAdapter = DtcDbAdapter.getInstance(context.getApplicationContext());
		mDBAdapter.open();
		ArrayList<DtcInfo> infoList = mDBAdapter.getInfoList();

		for (int i = 0; i < infoList.size(); i++) {
			DtcInfo info = infoList.get(i);
			RbLog.d("XML", info.getCode() + " : " + info.getInfoKor());
		}

		mDBAdapter.close();

	}

	public static void testDB3(Context context) {
		DiagDbUtil mDBAdapter = null;

		ArrayList<DiagInfo> infoList = mDBAdapter.getInfoList();

		for (int i = 0; i < infoList.size(); i++) {
			DiagInfo info = infoList.get(i);
			RbLog.d("XML", info.getCode() + " : " + info.getWorkTime());
		}

	}

	public static void insertHistoryDB(Context context, String code) {
		DiagDbUtil adapter = null;
		DiagInfo info = new DiagInfo();
		info.setCode(code);

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		info.setWorkTime(simpleDateFormat.format(date).toString());

		if (adapter.checkDuplication(code) == false) {
			RbLog.d(tag, "NOT DUPLICATED");
			adapter.insert(info);
		} else {
			RbLog.d(tag, "IS DUPLICATED");
		}

	}

	public static String getCurrentTimeString() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd");
		Date date = new Date();
		return simpleDateFormat.format(date).toString();
	}

}
